package lang;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

public class String_ {
    public static void main(String[] args) {

        char[] a = {'a', 'b', 'c'};

        String b = new String(a, 1, 1);


    }


    @Test //replace
    public void test1() {
        String a = "gdejicbegh";
        int acode = a.hashCode();

        String b = "hgebcijedg";
        int bcode = a.hashCode();


        String aaa = "xxxaaa";

        System.out.println(aaa.replace('a', 'c'));
        System.out.println(aaa.replace("a", "c"));

        String ccc = "a1a2a3a4";
        System.out.println(ccc.replaceAll("\\d", ""));
    }


    @Test //trim 空格处理
    public void test2() {
        char a = ' ';//英文　32
        char b = ' ';//中文  32
        char c = '　';//英文全角 12288
        char d = '　';//中文全角 12288
        System.out.println(Arrays.asList(a, b, c));

        String dd = "   a　　";
        String z = dd.trim();

        String zz = StringUtils.deleteWhitespace(dd);

    }

    @Test
    public void test3() {
        String a = "aa";
        String b = new String("aa").intern();
        System.out.println(a == b);
    }

    @Test
    public void test4() {

        System.out.println(String.join(",", "1", "2"));
    }

    //速度测试
    @Test
    public void split() {
        String ip = "192,168,20,121,123,123,123,123法法,法师打发,啊所发生的,阿法迪三,法师打发,大师傅3123,fasdf法法,adsffa,12312312313";

        String[] aaa = ip.split(",");
        System.out.println("第一次躲避常量池");
        for (int i = 0; i < aaa.length; i++) {
            System.out.print(aaa[i] + "  ");
        }


        long st1 = System.nanoTime();
        String[] ips = ip.split(",");
        System.out.println("使用jdk的split切分字符串");
        System.out.println("花费时间" + (System.nanoTime() - st1));


        long st2 = System.nanoTime();
        ips = StringUtils.split(ip, ",");
        System.out.println("使用common的split切分字符串");
        System.out.println("花费时间" + (System.nanoTime() - st2));

        long st3 = System.nanoTime();
        System.out.println("使用StringTokenizer的切分字符串");
        StringTokenizer token = new StringTokenizer(ip, ",");
        System.out.println("花费时间" + (System.nanoTime() - st3));

        long st4 = System.nanoTime();
        Pattern pattern = Pattern.compile(",");
        ips = pattern.split(ip);
        System.out.println("使用jdk的pattern切分字符串");
        System.out.println("花费时间" + (System.nanoTime() - st4));

    }


    /**
     * 编码问题解决
     * 思路
     * gbk 或者 ISO-8859-1 先找码表  他们对应的码表转换成 Unicode
     * Unicode 再转换成 他的子集
     * utf-8 2-3 byte
     * 16  2-4 byte
     * 32  固定 4byte
     */
    @Test
    public void test5() throws UnsupportedEncodingException {
        String str = "再见";

        //utf-8 原来的编码
        System.out.println(Arrays.toString(str.getBytes()));

        //转换成gbk
        for (byte b : str.getBytes("GBK"))
            System.out.printf("0x%x ", b);

        //转换成Unicode
        String un = new String(new byte[]{(byte) 0xd4, (byte) 0xd9, (byte) 0xbc, (byte) 0xfb}, "GBK");

        System.out.println(un);

        //Unicode 转 utf-8
        System.out.println(Arrays.toString(un.getBytes(StandardCharsets.UTF_8)));
    }
}
